﻿
using AntdUI;
using DABLL;
using DADAL;
using Microsoft.IdentityModel.Tokens;

namespace DataAcquisition
{
    public partial class DevicePointSelectForm : BaseForm
    {

        #region 属性

        private string EncryptionKey = SqlSugarHelper.EncryptionKey;

        /// <summary>
        /// 当前页码
        /// </summary>
        private int PageNumber = 1;

        /// <summary>
        /// 每页数量
        /// </summary>
        private int PageSize = 10;

        /// <summary>
        /// 总数量
        /// </summary>
        private int TotalPages;

        #endregion


        #region 自定义方法

        /// <summary>
        /// 布局
        /// </summary>
        private void SetupForm()
        {
            Table1.Height = Height - Header.Height - Divider2.Height - Pagination1.Height - 5;
            Table1.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("Number","点位编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Name","点位名称",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Address","点位地址",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PointType","点位类型",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Range","量程范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("OutValue","输出范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Alarm","报警范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("ErrorRange","判断范围",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Amend","修正数值",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Unit","数据单位",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("DeviceNumber","设备编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("DeviceName","设备名称",AntdUI.ColumnAlign.Center),
            };
        }

        /// <summary>设备点位列表
        /// 
        /// </summary>
        public void LoadList()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            List<DevicePointModel> list = DevicePoint.DevicePointList(where, PageNumber, PageSize, ref TotalPages);
            UpdateTable(list);
            Pagination1.Total = TotalPages;
            Pagination1.PageSize = PageSize;
        }

        /// <summary>更新Table
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<DevicePointModel> list)
        {
            var antItemList = new List<DevicePointItem>();
            foreach (DevicePointModel item in list)
            {
                string decrypt = string.Empty;
                try
                {
                    decrypt = Encryption.DesDecrypt(item.Address, EncryptionKey);
                }
                catch (Exception)
                {
                    Log.Error(string.Format("【{0}】点位地址加载失败", item.Name));
                }

                if (!decrypt.IsNullOrEmpty())
                {
                    string range = item.RangeMin.ToString() + "-" + item.RangeMax.ToString();
                    string outValue = item.OutMin.ToString() + "-" + item.OutMax.ToString();
                    string alarm = item.AlarmMin.ToString() + "-" + item.AlarmMax.ToString();
                    string pointType = item.PointType;
                    antItemList.Add(new DevicePointItem(item.DeviceNumber, item.DeviceName, item.Number,
                                                        item.Name, decrypt, item.PointType, range, outValue, alarm,
                                                        item.ErrorRange.ToString(), item.Amend.ToString(), item.Unit));
                }
            }
            Table1.DataSource = antItemList;
        }

        #endregion

        #region 系统方法

        public DevicePointSelectForm()
        {
            InitializeComponent();
            FullScreen = true;
            Header.Text = "设备点位选择";
        }

        private void DevicePointSelectForm_Load(object sender, EventArgs e)
        {
            SetupForm();
            Table1.Spin("正在加载中...", (config) =>
            {
                LoadList();
            });
        }
        #endregion

        private void DevicePointSelectForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.Owner is not null)
            {
                FormPointAddForm frm = (FormPointAddForm)this.Owner;
                frm.Fresh();
            }
            this.Dispose();
        }

        private void Pagination1_ValueChanged(object sender, PagePageEventArgs e)
        {
            PageNumber = e.Current;
            Task.Run(() =>
            {
                LoadList();
            });
        }

        private void Table1_CellDoubleClick(object sender, TableClickEventArgs e)
        {
            if (e.Record is DevicePointItem data)
            {
                if (this.Owner  is not null)
                {
                    FormPointAddForm frm = (FormPointAddForm)this.Owner;
                    frm.DevicePointItemService = data;
                }
            }
            this.Close();
        }
    }
}
